<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/10/10
 * Time: 20:00
 */

namespace app\admin\model;
use think\Config;
use think\Db;
use think\Exception;
use think\Model;
use app\admin\model\Ctrip;

class Ctripinfo extends Model
{
    /**
     * 门票可订性检查
     */
    public static function inspectTicket($resouceid,$time,$num,$price,$contactinfo,$passenerinfolist){
        $contactinfo = json_decode($contactinfo,true) ;
        if($passenerinfolist == null || $passenerinfolist==''){
            $passenerinfolist = [];
        }else{
            $passenerinfolist  = json_decode($passenerinfolist,true);
        }

        $res = Ctrip::send('menpiaokedingxingjiancha',[
            'ResourceInfoList'=>[[
                "ResourceID"=>$resouceid,
                "Quantity"=>$num,
                "UseDate"=>$time,
                "Price"=>$price,
            ]],
            'UID'=>Config::get("ctrip.uid"),
            'ContactInfo'=> $contactinfo,
            'PassenerInfoList'=>$passenerinfolist,
            'Amount'=> bcmul($price,$num,2), //(资源单价*资源数量)
            'PayMode'=>'P',
            'DistributionChannelID'=>9,
            'SID'=>Config::get("ctrip.sid"),
            'AllianceID'=>Config::get("ctrip.aid")
        ]);
       return $res;
    }
    /**
     * 携程下单
     */
    public static function Ordercreate($order_no,$scenicspotid,$resouceid,$time,$num,$price,$cx_num,$contactinfo,$passenerinfolist,$itemaddinfos){
        $contactinfo = json_decode($contactinfo,true) ;

        if($passenerinfolist == null || $passenerinfolist==''){
            $passenerinfolist = [];
        }else{
            $passenerinfolist  = json_decode($passenerinfolist,true);
        }

        if($itemaddinfos == null || $itemaddinfos ==''){
            $itemaddinfos = [];
        }else{
            $itemaddinfos  = json_decode($itemaddinfos,true);
        }
        $uid = self::userUid($contactinfo['Mobile'],$contactinfo['Name']);
        Db::name('order')->where('order_no',$order_no)->update(['uid'=>$uid]);
        $res = Ctrip::send('menpiaochuangjiandingdan',[
            'UserInfo'=>[
                'UID'=>$uid,
            ],
            'DistributionChannelID'=>9,
            'OrderFormInfo'=>[
                'Distributor_OrderID'=>$order_no,  //本地订单
                'ScenicSpotID'=>$scenicspotid,             //景点id
                'PayMode'=>'P',
                'ThirdPayType'=>3,                //预充值
                'PeopleNumber'=>$cx_num,                //出行人数
                'Amount'=>bcmul($price,$num,2),                 //价格
                'ServerFrom'=>'www.baidu.com',    //渠道地址
                'ResourceInfoList'=>[[
                    'ResourceID'=>$resouceid,     //资源id
                    'UseDate'=>$time,      //出行时间
                    'Quantity'=>$num,                //使用数量
                    'Price'=>$price,                //价格
                    'ItemAddInfos'=>$itemaddinfos,        //下单附加信息
                ]],
            ],
            'AllianceInfo'=>[
                'SID'=>Config::get("ctrip.sid"),
                'AllianceID'=>Config::get("ctrip.aid"),
                'OUID'=> 0,
                'AllianceName'=> "悦乐惠生活",
            ],
            'ContactInfo'=>$contactinfo,
            'PassengerInfoList'=>$passenerinfolist,
        ]);
        return  $res;
    }
    /**
     * 携程小程序订单
     */
    public static function xcorder($user_id,$order_no,$type,$resource_id,$amount,$num,$coupon_id,$scenicspotid,$price,$cx_num,$contactinfo,$passenerinfolist,$itemaddinfos,$time,$coupon_amount=0,$discount_amount=0,$title,$main_picture,$from=1,$flash_sale_id=0,$zone_id=0,$class_id=0,$user_mobile=0,$exchange_code='',$qinghai=0){
        //-------获取景点名称---
        $brand = Db::name("ctrip")->where(['scenicspotid'=>$scenicspotid])->value('name');
        if($zone_id){
            $operator_zone = Db::name('operator_zone')->where('id',$zone_id)->find();
            $from = 2;
        }else{
            $operator = Db::name('user')->where('id',$user_id)->value('operator');
            $operator_zone['operator_id'] = Db::name('operator')->where('name',$operator)->value('id');
            $operator_zone['vip_id'] = 0;
        }
        $arr = [
            'operator_id'=>$operator_zone['operator_id'],
            'vip_id'=>$operator_zone['vip_id'],
            'zone_id'=>$zone_id,
            'class_id'=>$class_id,
            'brand'=>$brand,
            'user_id'=>$user_id,
            'order_no'=>$order_no,
            'type'=>$type,
            'resource_id'=>$resource_id,
            'amount'=>$amount,
            'num'=>$num,
            'status'=>1,
            'from'=>$from,
            'coupon_id'=>$coupon_id,
            'scenicspotid'=>$scenicspotid,
            'price'=>$price,
            'cx_num'=>$cx_num,
            'contactinfo'=>$contactinfo,
            'passenerinfolist'=>$passenerinfolist,
            'itemaddinfos'=>$itemaddinfos,
            'createtime'=>date('Y-m-d H:i:s',time()),
            'time'=>$time,
            'coupon_amount'=>$coupon_amount,
            'discount_amount'=>$discount_amount,
            'title'=>$title,
            'main_picture'=>$main_picture,
            'flash_sale_id'=>$flash_sale_id,
            'user_mobile'=>$user_mobile,
            'exchange_code'=>$exchange_code,
            'qing_id'=>$qinghai,
        ];
        $order_id = Db::name('order')->insertGetId($arr);
        return $order_id;
    }

    //用户UID生成
    public static function userUid($number,$name){
        $res = Ctrip::send('yonghuuidshengcheng',[
            'TelphoneNumber'=>$number,
            'UidKey'=>$name,
            'SID'=>Config::get("ctrip.sid"),
            'AllianceID'=>Config::get("ctrip.aid")
        ]);
        return $res['UniqueUID'];
    }
}